package com.ct.gd.passport.view.policy;

import java.text.SimpleDateFormat;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.webkit.WebSettings;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.webkit.WebSettings.ZoomDensity;
import android.webkit.WebView;
import android.widget.TextView;

import com.ct.gd.passport.R;
import com.ct.gd.passport.common.BaseActivity;
import com.ct.gd.passport.domain.PolicyDetail;
import com.ct.gd.passport.view.BundleKeys;
import com.ct.gd.passport.view.widget.CustomHeaderFragment;
import com.up91.common.android.async.ILoading;
import com.up91.common.android.async.SimpleAsyncTask;

public class PolicyDetailActivity extends BaseActivity {
	static final String				MimeType		= "text/html";
	static final String				Encoding		= "utf-8";
	static final int				WEB_FONT_SIZE	= 24;

	private FragmentManager			mFgMan;
	private CustomHeaderFragment	mFgHeader;

	private TextView				mTVTitle;
	private TextView				mTVTime;
	private WebView					mWVContent;
	private SimpleDateFormat		mFormatter = new SimpleDateFormat("yyyy-MM-dd");

	@Override
	protected void onBaseCreate(Bundle savedInstanceState) {
		setContentView(R.layout.policy_detail);
	}

	@Override
	protected void initViews() {
		mFgMan = getSupportFragmentManager();

		mFgHeader = (CustomHeaderFragment) mFgMan.findFragmentById(R.id.header);
		mFgHeader.setCenterText(R.string.policy);

		mTVTitle = (TextView) findViewById(R.id.tv_policy_detail_title);
		mTVTime = (TextView) findViewById(R.id.tv_policy_detail_time);
		mWVContent = (WebView) findViewById(R.id.wv_policy_detail_content);
		WebSettings settings = mWVContent.getSettings();
		settings.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
		// settings.setDefaultFontSize((int)(WebViewConst.WEB_FONT_SIZE * 1.5));
		settings.setDefaultFontSize(WEB_FONT_SIZE);
		settings.setDefaultZoom(ZoomDensity.FAR);
	}

	@Override
	protected void onAfterCreate(Bundle savedInstanceStat) {
		int id = getIntent().getIntExtra(BundleKeys.CATALOG_ID, 0);
		new GetDetailTask((ILoading) this, true).execute(id);
	}

	private class GetDetailTask extends SimpleAsyncTask<Integer, Void, PolicyDetail> {

		public GetDetailTask(ILoading loading, boolean showLoading) {
			super(loading, showLoading);
		}

		@Override
		protected PolicyDetail onLoad(Integer... params) throws Exception {
			return PolicyDetail.load(params[0]);
		}

		@Override
		protected void onUI(PolicyDetail result) {
			mTVTitle.setText(result.getName());
			mTVTime.setText(result.getUpdatetime());

			// mWVContent.loadUrl(result.getContent());
			mWVContent.loadDataWithBaseURL(null, result.getContent(), MimeType, Encoding, null);
		}

	}

}
